
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="content-type" content="text/html" charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,minimal-ui" />
    <meta name="format-detection" content="telephone=no">
    <title>买入</title>
    <link rel="stylesheet" href="{{asset('/admin/layui/css/layui.css') }}" />
    <link rel="stylesheet" href="{{asset('/api/static/css/base.css')}}">
    <link rel="stylesheet" href="{{asset('/api/static/css/transaction.css')}}">

    <script>
        (function (doc, win) {
            var docEl = doc.documentElement,
                resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize',
                recalc = function () {
                    var clientWidth = docEl.clientWidth;
                    if (!clientWidth) return;
                    docEl.style.fontSize = 100 * (clientWidth / 1080) + 'px';
                };
            recalc();
            if (!doc.addEventListener) return;
            win.addEventListener(resizeEvt, recalc, false);
            doc.addEventListener('DOMContentLoaded', recalc, false);
        })(document, window);
    </script>
</head>
<body>
<div class="transaction">
    <div class="sumAdd clearfix">
        <div class="sum fl">
            <p>{{$currency_name}}可用资产<strong>{{$infos['balance']}}</strong></p>
            <p>{{$currency_name}}冻结资产<strong>{{$infos['blocked']}}</strong></p>
        </div>
        {{--<div class="add fr"><a href="javascript:;">充值</a></div>--}}
    </div>
    <div class="mianBox clearfix">
        <form class="form-horizontal" role="form" id="myform">
            {{csrf_field()}}
            <div class="fl textBox">
                <input type="hidden" name="cid" value="{{$id}}">
                <input type="hidden" name="uid" value="{{$uid}}">
                <input type="hidden" name="ps" value="{{$ps}}">
                <input type="hidden" name="oba" value="{{$oba}}">
        @if(!empty($uid))
            @if($type==1)
                <input type="hidden" name="type" value="1">
                <p><label>最佳买价</label><input type="text" name="" value="{{$list['buy_best']}}" readonly></p>
                <p><label>价格<span style="font-size: .35rem;">(USDT)</span></label><input class="inPrice" type="text" name="price" value="" placeholder="请输入价格"></p>
                <p><label>最大可买</label><input type="tel" name="" value="0" class="maxNum" readonly></p>
                <p><label>数量</label><input class="inNum" type="text" name="num" value="" placeholder="请输入数量"></p>
                <p><label>交易密码</label><input class="inPwd" type="password" name="password" maxlength="10" value="" placeholder="请输入密码"></p>
            @endif
            @if($type==2)
                <input type="hidden" name="type" value="2">
                <p><label>最佳卖价</label><input type="text" name="" value="{{$list['sell_best']}}" readonly></p>
                <p><label>价格<span style="font-size: .35rem;">(USDT)</span></label><input class="inPrice" type="text" name="price" value="" placeholder="请输入价格"></p>
                <p><label>最大可卖</label><input type="tel" name="" value="" class="maxSell" readonly></p>
                <p><label>数量</label><input class="inNum" type="text" name="num" value="" placeholder="请输入数量"></p>
                <p><label>交易密码</label><input class="inPwd" type="password" name="password" maxlength="10" value="" placeholder="请输入密码"></p>
            @endif

        @else
            @if($type==1)
                <input type="hidden" name="type" value="1">
                <p><label>最佳买价</label><input type="text" name="" value="{{$list['buy_best']}}" readonly></p>
                <p><label>价格<span style="font-size: .35rem;">(USDT)</span></label><input class="inPrice" type="text" name="price" value="" placeholder="请输入价格" readonly></p>
                <p><label>最大可买</label><input type="tel" name="" value="0" class="maxNum" readonly></p>
                <p><label>数量</label><input class="inNum" type="text" name="num" value="" placeholder="请输入数量" readonly></p>
                <p><label>交易密码</label><input class="inPwd" type="password" name="password" maxlength="10" value="" placeholder="请输入密码" readonly></p>
            @endif
            @if($type==2)
                <input type="hidden" name="type" value="2">
                <p><label>最佳卖价</label><input type="text" name="" value="{{$list['sell_best']}}" readonly></p>
                <p><label>价格<span style="font-size: .35rem;">(USDT)</span></label><input class="inPrice" type="text" name="price" value="" placeholder="请输入价格" readonly></p>
                <p><label>最大可卖</label><input type="tel" name="" value="" class="maxSell" readonly></p>
                <p><label>数量</label><input class="inNum" type="text" name="num" value="" placeholder="请输入数量" readonly></p>
                <p><label>交易密码</label><input class="inPwd" type="password" name="password" maxlength="10" value="" placeholder="请输入密码" readonly></p>
                @endif
        @endif   
            @if(!empty($uid))
            <div class="settlement">
                <p>成交额：0</p>
                @if($type==1)
                    <p>手续费： {{$currency_fee->buy_fee}}(USDT)</p>
                @endif
                @if($type==2)
                    <p>手续费：{{$currency_fee->sell_fee}}(USDT)</p>
                @endif
            </div>
            <div class="tranBtn">
                    @if($type==1)
                        <a href="javascript:;" onclick="on_add()">确认买入</a>
                    @endif
                    @if($type==2)
                        <a href="javascript:;" onclick="on_add()">确认卖出</a>
                    @endif
            </div>
            @else
            <div class="noLogin">
                <div class="noLoginImg">
                    <img src="{{asset('/api/static/images/header.png')}}">
                </div>
                <div class="gotoLogin subBtn">
                    <a href="javascript:;" onclick="ttp.goToLogin()">点击登录</a>
                </div>
                <p class="noLoginTipText">登陆后查看资产信息</p>
            </div>
            @endif
        </div>
        <div class="fl onlyData">
            <table border="0" class="sellData">
                @php
                    $a = count($list['sell'])
                @endphp
                @foreach($list['sell'] as $key=>$val)
                    <tr>
                        <td>卖{{$a-$key}}</td>
                        <td>{{$val['y_num']}}</td>
                        <td>{{$val['price']}}</td>
                    </tr>
                @endforeach
            </table>
            <i class="line"></i>
            <table border="0" class="buyData">
                @foreach($list['buy'] as $key=>$val)
                    <tr>
                        <td>买{{$key+1}}</td>
                        <td>{{$val['y_num']}}</td>
                        <td>{{$val['price']}}</td>
                    </tr>
                @endforeach
            </table>
        </div>
        </form>
    </div>
    <div class="tipsPwd hide">
        <div class="tipsCentent">
            <div class="tipsText">
                <p>还未设置交易密码</p>
            </div>
            <div class="tipsBtn clearfix">
                <a class="fl" href="javascript:;" onclick="showTips()">稍后再去</a>
                <a class="fr" href="javascript:;" onclick="goToSetPayPwd()">前往设置</a>
            </div>
        </div>
    </div>
</div>
</body>
<script src="{{asset('/api/static/js/jquery-1.10.2.min.js')}}"></script>
<script src="{{asset('/admin/layui/layui.all.js') }}"></script>
<script src="{{asset('/api/static/js/base.js?v=1')}}"></script>
<script src="{{asset('/js/socket.io.js') }}"></script>
<script>
    var balance = "{{$infos['balance']}}"=='-' ?  0 : {{$infos['balance']}}
    $(function(){
        $('form').serialize();
        if( {{$type}}==2 ){
            if(  $('input[name=oba]').val()=='1'  ){
                $('.maxSell').val( parseInt(balance) )
            } else {
                $('.maxSell').val( (parseFloat( balance ).toFixed(4)) )
            }
        }
        $('.inPrice').on('input propertychange', this, function(){
            var priceVal = $(this).val();
            if( priceVal>999999999 ){
                priceVal = 999999999
                $(this).val(priceVal)
            }
            $(this).val( floatNum(priceVal, 2) );
            var buyFee = parseFloat(suanBuyfee())
            if( $(this).val()=='' || parseFloat($(this).val())=='0' ){
                var danjia = '0'
            } else {
                if( $('input[name=oba]').val()=='1' ){
                    var danjia = Math.floor(parseFloat( balance/parseFloat($(this).val()) ))
                    $('.settlement').html("<p>成交额： "+ (($(this).val()*$('.inNum').val()).toFixed(2)) +"</p><p>手续费： "+(buyFee.toFixed(4))+"(USDT)</p>")
                } else {
                    var danjia = parseFloat( balance/parseFloat($(this).val()) ).toFixed(4)
                    $('.settlement').html("<p>成交额： "+ (($(this).val()*$('.inNum').val()).toFixed(6)) +"</p><p>手续费： "+(buyFee.toFixed(8))+"(USDT)</p>")
                }
            }
            $(this).parents('.textBox').find('.maxNum').val( danjia )
            // console.log(balance, danjia, $('.maxNum').val(), $(this).val())
        });
        $('.inNum').on('input propertychange', this, function(){
            var numVal = $(this).val();
            if( numVal>9999999999 ){
                numVal = 9999999999
                $(this).val(numVal)
                console.log( $(this).val() )
            }
            var buyFee = parseFloat(suanBuyfee())
            if( $('input[name=oba]').val()=='1' ){
                $(this).val( onlyNum(numVal) );
                $('.settlement').html("<p>成交额："+ (($(this).val()*$('.inPrice').val()).toFixed(2)) +"</p><p>手续费： "+(buyFee.toFixed(4))+"(USDT)</p>")
            } else {
                $(this).val( floatNum(numVal, 4) );
                $('.settlement').html("<p>成交额："+ (($(this).val()*$('.inPrice').val()).toFixed(6)) +"</p><p>手续费： "+(buyFee.toFixed(8))+"(USDT)</p>")
            }
        });
    });
    function showTips() {
        if( $('.tipsPwd').hasClass('hide') ){
            $('.tipsPwd').removeClass('hide')
        } else {
            $('.tipsPwd').addClass('hide')
        }
    }
    function goToSetPayPwd () {
        $('.tipsPwd').addClass('hide')
        window.ttp.goToSetPayPwd()
    }
    function suanBuyfee() {
        if( {{$type}}==1 ){
            return (($('.inNum').val()*{{$currency_fee->buy_fee}}*$('.inPrice').val())/100).toFixed(8) || {{$currency_fee->buy_fee}}
        } else {
            return (($('.inNum').val()*{{$currency_fee->sell_fee}}*$('.inPrice').val())/100).toFixed(8) || {{$currency_fee->sell_fee}}
        }
    }
    function on_add() {
        if( $("input[name='price']").val()=='' || $("input[name='price']").val()==0 ){
            layer.msg('请输入大于0的价格',{icon: 2})
            return
        }
        if( $("input[name='num']").val()=='' || $("input[name='num']").val()<=0 ){
            layer.msg('请输入大于0的数量',{icon: 2})
            return
        }
        if( $("input[name='password']").val()=='' ){
            layer.msg('请输入密码',{icon: 2})
            return
        }
        if( $("input[name='password']").val().length<6 ){
            layer.msg('请输入6位数字密码',{icon: 2})
            return
        }
        $.ajax({
            type : "POST",
            url:'/api/entrust_create',
            data : $('form').serialize(),
            success: function(res){
                if(res.status == 1){
                    $('#myform').get(0).reset()
                    layer.msg('添加成功',{icon:1})
                    $.ajax({
                        type : "GET",
                        url:'/api/test?id={{$id}}',
                        success: function(res){
                            setTimeout(function(){
                                window.location.reload()
                            }, 1500)
                        }
                    });
                } else if( res.status == 2 ){
                    showTips()
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }
        });
    }
    function html2Escape(sHtml) {
        return sHtml.replace(/[<>&"]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;'}[c];});
    }
    function escape2Html(str) {
        var arrEntities={'lt':'<','gt':'>','nbsp':' ','amp':'&','quot':'"'};
        return str.replace(/&(lt|gt|nbsp|amp|quot);/ig,function(all,t){return arrEntities[t];});
    }
    var socket = io('https://a.51tuanli.com:2120');
    uid = 'c_m';
    socket.on('connect', function(){
        socket.emit('login', uid);
    });
    // 后端推送来消息时
    socket.on('new_msg', function(msg){
        var text = escape2Html(msg);
        var data = JSON.parse(text);
        if( data.cid=="{{$id}}" ){
            var sellDataList = '',
                buyDataList = '';
            for( var i in data.sell ){
                sellDataList += '<tr>'
                               +'<td>卖'+ (parseInt(i)+1) +'</td>'
                               +'<td>' + data.sell[i].y_num + '</td>'
                               +'<td>' + data.sell[i].price + '</td>'
                               +'</tr>'
            }
            for( var i in data.buy ){
                buyDataList += '<tr>'
                               +'<td>买'+ (parseInt(i)+1) +'</td>'
                               +'<td>' + data.buy[i].y_num + '</td>'
                               +'<td>' + data.buy[i].price + '</td>'
                               +'</tr>'
            }
            $(document).find('.sellData').html( sellDataList )
            $(document).find('.buyData').html( buyDataList )
        }
    });
</script>
</html>
